package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.pojo.Loanapplications;
import com.cashmate.hibernate.pojo.Loanverification;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;

public class Disapproval_SaveLoan extends ActionSupport {

    private LoanverificationDAO loanappDAO = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private String disselected;
    private String loc;

    @Actions({
        @Action(value = "/saveloandis", results = {
            @Result(location = "${loc}", name = "success", type = "redirect"),
            @Result(location = "${loc}", name = "input")
        })
    })
    @Override
    public String execute() throws Exception {

        loc = "/loanapproval.jsp?cat=request";
        List<Loanverification> loanapps = new ArrayList<Loanverification>();
        String[] sel = disselected.split(",");
        for (String s : sel) {
            try {
                int id = Integer.parseInt(s);
                loanapps.add(loanappDAO.get(id));
            } catch (Exception e) {
                return ERROR;
            }
        }

        for (Loanverification l : loanapps) {
            l.setStatus("Disapproved");
            loanappDAO.update(l);
        }

        return SUCCESS;
    }

    public void setDisselected(String disselected) {
        this.disselected = disselected;
    }

    public String getLoc() {
        return loc;
    }
}
